package com.novellotus.iov.carvice.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.ChartRecyclerAdapter;
import com.novellotus.iov.carvice.bean.RxDiagnosisChart;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * A simple {@link Fragment} subclass.
 */
public class ChartFragment extends BaseFragment {


    @BindView(R.id.recyclerChart)
    RecyclerView recyclerChart;
    ChartRecyclerAdapter chartRecyclerAdapter;
    private String vin = "", e = "", r = "", time = "";

    public ChartFragment() {
        // Required empty public constructor
    }

    public static ChartFragment newInstance(String vin, String e, String r, String time) {
        ChartFragment fragment = new ChartFragment();
        Bundle args = new Bundle();
        args.putString("vin", vin);
        args.putString("e", e);
        args.putString("r", r);
        args.putString("time", time);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            vin = getArguments().getString("vin");
            e = getArguments().getString("e");
            r = getArguments().getString("r");
            time= getArguments().getString("time");
        }
    }

    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_chart, container, false);
    }

    @Override
    void init() {

        recyclerChart.setLayoutManager(MyViewUtils.getLinearLayoutManager(getContext()));
        getDiagnosisData();
    }

    /**
     *
     */
    public void getDiagnosisData() {
        Observable<BaseArrayEntity<RxDiagnosisChart>> observable = RetrofitFactory.getInstance().alarmsChartDetail(vin, SharedPreferencesUtils.getKey(getContext())[0], time, e, r);
        observable.compose(RxSchedulers.<BaseArrayEntity<RxDiagnosisChart>>compose())
                .subscribe(new BaseArrayObserver<RxDiagnosisChart>(getContext()) {

                    @Override
                    public void onSuccess(List<RxDiagnosisChart> list) {
                        chartRecyclerAdapter = new ChartRecyclerAdapter(list);
                        recyclerChart.setAdapter(chartRecyclerAdapter);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
    }

}
